﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace RssReader
{
    public class VerticalOffsetBehavior : DependencyObject
    {


        public static double GetOffset(DependencyObject obj)
        {
            return (double)obj.GetValue(OffsetProperty);
        }

        public static void SetOffset(DependencyObject obj, double value)
        {
            obj.SetValue(OffsetProperty, value);
        }

        // Using a DependencyProperty as the backing store for Offset.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OffsetProperty =
            DependencyProperty.RegisterAttached("Offset", typeof(double), typeof(VerticalOffsetBehavior), new UIPropertyMetadata(0.0, 
                new PropertyChangedCallback( OnOffsetChanged )  
                ));

        public static void OnOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ScrollViewer sc = d as ScrollViewer;
            if (sc != null)
            {
                System.Diagnostics.Debug.WriteLine ("Scrolling to " + e.NewValue.ToString()) ;
                sc.ScrollToVerticalOffset((double)e.NewValue); 
            } 

        } 


    } 

    public class WebBrowserExtender : DependencyObject
    {



        public static string GetHTML(DependencyObject obj)
        {
            return (string)obj.GetValue(HTMLProperty);
        }

        public static void SetHTML(DependencyObject obj, string value)
        {
            obj.SetValue(HTMLProperty, value);
        }

        // Using a DependencyProperty as the backing store for HTML.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HTMLProperty =
            DependencyProperty.RegisterAttached("HTML", typeof(string), typeof(WebBrowserExtender), new UIPropertyMetadata(null,
                new PropertyChangedCallback(HTMLChanged)));

        public static void HTMLChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            WebBrowser wb = d as WebBrowser;
            if (wb != null)
            {
                if (e.NewValue is string)
                {
                    StringBuilder sb = new StringBuilder(); 
                    sb.Append ( "<HTML><HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></HEAD><BODY>" ); 
                    sb.Append ( e.NewValue ) ; 
                    sb.Append ( "</BODY><HTML>"); 
                     
                    wb.NavigateToString(sb.ToString() );
                }
            }
        }
    }

    public class RichTextBoxFlowDocumentSetter
    {


        public static FlowDocument GetDocument(DependencyObject obj)
        {
            return (FlowDocument)obj.GetValue(DocumentProperty);
        }

        public static void SetDocument(DependencyObject obj, FlowDocument value)
        {
            obj.SetValue(DocumentProperty, value);
        }

        // Using a DependencyProperty as the backing store for Document.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DocumentProperty =
            DependencyProperty.RegisterAttached("Document", typeof(FlowDocument), typeof(RichTextBoxFlowDocumentSetter), new UIPropertyMetadata(null, 
                    new PropertyChangedCallback (OnDocumentChanged) 
                ));

        public static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RichTextBox wb = d as RichTextBox;
            if (wb != null)
            {
                wb.Document = e.NewValue as FlowDocument ;                  
            }
        }
    } 

}
